// adapter/persistence/BorrowRecordRepositoryAdapter.java
package com.library.bookservice.adapter.persistence;

import com.library.bookservice.domain.model.BorrowRecord;
import com.library.bookservice.domain.port.BorrowRecordRepositoryPort;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 借阅记录仓库适配器
 * 实现BorrowRecordRepositoryPort接口，连接JPA仓库
 */
@Component
public class BorrowRecordRepositoryAdapter implements BorrowRecordRepositoryPort {

    private final BorrowRecordJpaRepository borrowRecordJpaRepository;

    public BorrowRecordRepositoryAdapter(BorrowRecordJpaRepository borrowRecordJpaRepository) {
        this.borrowRecordJpaRepository = borrowRecordJpaRepository;
    }

    @Override
    public BorrowRecord save(BorrowRecord record) {
        BorrowRecordEntity entity = toEntity(record);
        BorrowRecordEntity savedEntity = borrowRecordJpaRepository.save(entity);
        return toDomain(savedEntity);
    }

    @Override
    public BorrowRecord findById(Long id) {
        return borrowRecordJpaRepository.findById(id)
                .map(this::toDomain)
                .orElseThrow(() -> new RuntimeException("借阅记录不存在"));
    }

    @Override
    public List<BorrowRecord> findByBookId(Long bookId) {
        return borrowRecordJpaRepository.findByBookId(bookId).stream()
                .map(this::toDomain)
                .collect(Collectors.toList());
    }

    @Override
    public List<BorrowRecord> findByBorrowerName(String borrowerName) {
        return borrowRecordJpaRepository.findByBorrowerName(borrowerName).stream()
                .map(this::toDomain)
                .collect(Collectors.toList());
    }

    @Override
    public List<BorrowRecord> findByNotReturned() {
        return borrowRecordJpaRepository.findByReturnedFalse().stream()
                .map(this::toDomain)
                .collect(Collectors.toList());
    }

    @Override
    public void deleteById(Long id) {
        borrowRecordJpaRepository.deleteById(id);
    }

    /**
     * 将领域对象转换为JPA实体
     */
    private BorrowRecordEntity toEntity(BorrowRecord record) {
        BorrowRecordEntity entity = new BorrowRecordEntity();
        entity.setId(record.getId());
        entity.setBookId(record.getBookId());
        entity.setBorrowerName(record.getBorrowerName());
        entity.setBorrowerPhone(record.getBorrowerPhone());
        entity.setBorrowDate(record.getBorrowDate());
        entity.setReturnDate(record.getReturnDate());
        entity.setReturned(record.isReturned());
        return entity;
    }

    /**
     * 将JPA实体转换为领域对象
     */
    private BorrowRecord toDomain(BorrowRecordEntity entity) {
        BorrowRecord record = new BorrowRecord();
        record.setId(entity.getId());
        record.setBookId(entity.getBookId());
        record.setBorrowerName(entity.getBorrowerName());
        record.setBorrowerPhone(entity.getBorrowerPhone());
        record.setBorrowDate(entity.getBorrowDate());
        record.setReturnDate(entity.getReturnDate());
        record.setReturned(entity.isReturned());
        return record;
    }
}